using System;
using System.Data;

namespace SharpObjects.Data
{

    public class DbReaderRow
    {
        protected IDataReader m_rd;

        public DbReaderRow(IDataReader dr)
        {
            m_rd = dr;
            m_strprop = new StringProperty(this);
            m_intprop = new IntegerProperty(this);
            m_longprop = new LongProperty(this);
            m_dblprop = new DoubleProperty(this);
            m_dtprop = new DateTimeProperty(this);
            m_boolprop = new BooleanProperty(this);
        }

        public IDataReader Row { get { return m_rd; } }

        public bool GetBoolean(string sColumn)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
            }
            string sValue = m_rd[sColumn].ToString().Trim();

            if (sValue == "0")
            {
                return false;
            }
            else if (sValue == "1")
            {
                return true;
            }
            else if (sValue.ToUpper() == "Y")
            {
                return true;
            }
            else if (sValue.ToUpper() == "N")
            {
                return false;
            }
            else if (sValue.ToUpper() == "T")
            {
                return true;
            }
            else if (sValue.ToUpper() == "F")
            {
                return false;
            }
            else
            {
                return Convert.ToBoolean(sValue);
            }
        }

        public bool GetBoolean(string sColumn, bool DefaultValue)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                return DefaultValue;
            }
            return this.GetBoolean(sColumn);
        }

        protected BooleanProperty m_boolprop;
        public BooleanProperty BooleanValue { get { return m_boolprop; } }

        public DateTime GetDateTime(string sColumn)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
            }
            return (DateTime)m_rd[sColumn];
        }

        public DateTime GetDateTime(string sColumn, DateTime DefaultValue)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                return DefaultValue;
            }
            return this.GetDateTime(sColumn);
        }

        protected DateTimeProperty m_dtprop;
        public DateTimeProperty DateTimeValue { get { return m_dtprop; } }

        public double GetDouble(string sColumn)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
            }
            string sValue = m_rd[sColumn].ToString().Trim();
            return Convert.ToDouble(sValue);
        }

        public double GetDouble(string sColumn, double DefaultValue)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                return DefaultValue;
            }
            return this.GetDouble(sColumn);
        }

        protected DoubleProperty m_dblprop;
        public DoubleProperty DoubleValue { get { return m_dblprop; } }

        public decimal GetDecimal(string sColumn)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
            }
            string sValue = m_rd[sColumn].ToString().Trim();
            return Convert.ToDecimal(sValue);
        }

        public decimal GetDecimal(string sColumn, decimal DefaultValue)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                return DefaultValue;
            }
            return this.GetDecimal(sColumn);
        }

        protected DecimalProperty m_decprop;
        public DecimalProperty DecimalValue { get { return m_decprop; } }

        public int GetInteger(string sColumn)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
            }
            string sValue = m_rd[sColumn].ToString().Trim();
            return Convert.ToInt32(sValue);
        }

        public int GetInteger(string sColumn, int DefaultValue)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                return DefaultValue;
            }
            return this.GetInteger(sColumn);
        }

        protected IntegerProperty m_intprop;
        public IntegerProperty IntegerValue { get { return m_intprop; } }

        public long GetLong(string sColumn)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
            }
            string sValue = m_rd[sColumn].ToString().Trim();
            return Convert.ToInt64(sValue);
        }

        public long GetLong(string sColumn, long DefaultValue)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                return DefaultValue;
            }
            return this.GetLong(sColumn);
        }

        protected LongProperty m_longprop;
        public LongProperty LongValue { get { return m_longprop; } }

        public string GetString(string sColumn)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                throw new Exception(string.Format("The value of the column \"{0}\" is NULL", sColumn));
            }
            return m_rd[sColumn].ToString();
        }

        public string GetString(string sColumn, string DefaultValue)
        {
            if(m_rd[sColumn] == null || m_rd[sColumn] == DBNull.Value)
            {
                return DefaultValue;
            }
            return this.GetString(sColumn);
        }

        protected StringProperty m_strprop;
        public StringProperty StringValue { get { return m_strprop; } }

        public class StringProperty
        {
            protected DbReaderRow m_row;
            public StringProperty(DbReaderRow row) { m_row = row; }

            public string this[string sColumn] { get { return m_row.GetString(sColumn); } }
            public string this[string sColumn, string default_value] { get { return m_row.GetString(sColumn, default_value); } }
        }

        public class IntegerProperty
        {
            protected DbReaderRow m_row;
            public IntegerProperty(DbReaderRow row) { m_row = row; }

            public int this[string sColumn] { get { return m_row.GetInteger(sColumn); } }
            public int this[string sColumn, int default_value] { get { return m_row.GetInteger(sColumn, default_value); } }
        }

        public class LongProperty
        {
            protected DbReaderRow m_row;
            public LongProperty(DbReaderRow row) { m_row = row; }

            public long this[string sColumn] { get { return m_row.GetLong(sColumn); } }
            public long this[string sColumn, long default_value] { get { return m_row.GetLong(sColumn, default_value); } }
        }

        public class DoubleProperty
        {
            protected DbReaderRow m_row;
            public DoubleProperty(DbReaderRow row) { m_row = row; }

            public double this[string sColumn] { get { return m_row.GetDouble(sColumn); } }
            public double this[string sColumn, double default_value] { get { return m_row.GetDouble(sColumn, default_value); } }
        }

        public class DecimalProperty
        {
            protected DbReaderRow m_row;
            public DecimalProperty(DbReaderRow row) { m_row = row; }

            public decimal this[string sColumn] { get { return m_row.GetDecimal(sColumn); } }
            public decimal this[string sColumn, decimal default_value] { get { return m_row.GetDecimal(sColumn, default_value); } }
        }

        public class DateTimeProperty
        {
            protected DbReaderRow m_row;
            public DateTimeProperty(DbReaderRow row) { m_row = row; }

            public DateTime this[string sColumn] { get { return m_row.GetDateTime(sColumn); } }
            public DateTime this[string sColumn, DateTime default_value] { get { return m_row.GetDateTime(sColumn, default_value); } }
        }

        public class BooleanProperty
        {
            protected DbReaderRow m_row;
            public BooleanProperty(DbReaderRow row) { m_row = row; }

            public bool this[string sColumn] { get { return m_row.GetBoolean(sColumn); } }
            public bool this[string sColumn, bool default_value] { get { return m_row.GetBoolean(sColumn, default_value); } }
        }

    }




}
